﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using RealmMudLib.Screens.Base;

namespace RealmMudLib.Managers
{
    public class ScreenManager
    {
        public Game BaseGame { get; set; }
        public InputManager Input { get; set; }
        public List<GameScreen> Screens { get; set; }

        public ScreenManager(Game game)
        {
            this.BaseGame = game;
            Input = new InputManager();
            this.Screens = new List<GameScreen>();
        }
        public void LoadContent()
        {
            foreach (GameScreen screen in Screens)
            {
                screen.LoadContent();
            }
        }
        public void Update(GameTime gameTime)
        {
            // Cant update a enumerable while looking through it, so work on a copy
            List<GameScreen> _screens = new List<GameScreen>();

            foreach (GameScreen screen in Screens)
            {
                _screens.Add(screen);
            }

            for (int i = 0; i < _screens.Count; i++)
            {
                // Grab the screen and update it
                _screens[i].Update(gameTime);
            }

            // Readd them the correct way
            Screens.Clear();
            foreach (GameScreen screen in _screens)
            {
                Screens.Add(screen);
            }
            // Now all our screens are updated, update our inputmanager next
            Input.Update(gameTime);
        }
        public void Draw(GameTime gameTime)
        {
            // Draw all our screens
            foreach (GameScreen screen in Screens)
            {
                screen.Draw(gameTime);
            }
        }
        public bool AddScreen(GameScreen screen)
        {
            if (!this.Screens.Contains(screen))
            {
                this.Screens.Add(screen);
                return true;
            }
            else { return false; }
        }
        public bool RemoveScreen(GameScreen screen)
        {
            if (this.Screens.Contains(screen))
            {
                this.Screens.Remove(screen);
                return true;
            }
            else { return false; }
        }
    }
}
